package com.svms.save.services;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.svms.save.config.SvmsSaveConfig;
import com.svms.save.entitys.SvdaYhb;
import com.svms.save.entitys.SvdaZd;
import com.svms.save.entitys.SvdaZdExample;
import com.svms.save.mapper.SvdaZdMapper;
import com.svms.save.utils.PinYinUtils;
import com.svms.save.utils.SysCode;
import com.svms.save.vo.ZdVo;

public abstract class ZdService extends BaseService{
	@Autowired
	private SvdaZdMapper zdMapper;
	@Autowired
	private SvmsSaveConfig config;
	public List<ZdVo> getZd(){
		return config.getEditZd();
	}
	
	public List<SvdaZd> findZdByLxjp(String lxjp){
		List<SvdaZd> list = null;
		if(StringUtils.isNotBlank(lxjp)) {
			SvdaZdExample ex = new SvdaZdExample();
			SvdaZdExample.Criteria cri =  ex.createCriteria();
			cri.andLxjpEqualTo(lxjp);
			cri.andCzbsNotEqualTo(SysCode.CZBS_DELETE);
			ex.setOrderByClause("zddm asc");
			list = zdMapper.selectByExample(ex);
		}
		return list;
	}
	
	public SvdaZd detailByOid(Long objectid) {
		return zdMapper.selectByPrimaryKey(objectid);
	}
	
	public int doSave(SvdaYhb yh,SvdaZd zd) {
		super.setEntityDefVal(yh, zd);
		int r = 0;
		String zdjp = PinYinUtils.getPinYinHeadChar(zd.getZdmc());
		zd.setZdjp(zdjp);
		if(null == zd.getObjectid()) {
			r = zdMapper.insert(zd);
			zd.setCzbs(SysCode.CZBS_ADD);
		}else {
			r = zdMapper.updateByPrimaryKey(zd);
			zd.setCzbs(SysCode.CZBS_UPDATE);
		}
		return r;
	}
	
	public int deleteZdItem(SvdaZd zd) {
		zd.setCzbs(SysCode.CZBS_DELETE);
		int r = zdMapper.updateByPrimaryKeySelective(zd);
		return r;
	}
	
	public SvdaZd getZdByLxjpZddm(String lxjp,String zddm) {
		SvdaZd zd = null;
		if(StringUtils.isNotBlank(lxjp) && StringUtils.isNotBlank(zddm)) {
			SvdaZdExample ex = new SvdaZdExample();
			SvdaZdExample.Criteria cri =  ex.createCriteria();
			cri.andLxjpEqualTo(lxjp);
			cri.andZddmEqualTo(zddm);
			cri.andCzbsNotEqualTo(SysCode.CZBS_DELETE);
			ex.setOrderByClause("zddm asc");
			List<SvdaZd> list = zdMapper.selectByExample(ex);
			if(list.size() > 0) {
				zd = list.get(0);
			}
		}
		return zd;
	}
}
